package com.haotunet.app.goldtreasure.fragment;

import android.support.v4.app.Fragment;

import com.haotunet.app.goldtreasure.utils.ActivityUtils;
import com.umeng.analytics.MobclickAgent;

public abstract class BaseFragment extends Fragment {

    /**
     * Fragment当前状态是否可见
     */
    protected boolean isVisible;

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        if (getUserVisibleHint()) {
            isVisible = true;
            onVisible();
            MobclickAgent.onPageStart(ActivityUtils.getCurrentFragmentName(BaseFragment.this));
        } else {
            isVisible = false;
            onInvisible();
            MobclickAgent.onPageEnd(ActivityUtils.getCurrentFragmentName(BaseFragment.this));
        }
    }


    /**
     * 可见
     */
    protected void onVisible() {
        lazyLoad();
    }


    /**
     * 不可见
     */
    protected void onInvisible() {


    }


    /**
     * 延迟加载
     * 子类必须重写此方法
     */
    protected abstract void lazyLoad();

    /**
     * 判断Activity是否可用
     * @return activity是否可用
     */
    protected boolean isActivityAvailable() {
        return getActivity() != null && this.isAdded() && !getActivity().isFinishing() ;
    }
}
